package com.lebang.controller.api.earned;

import com.lebang.common.Require;
import com.lebang.common.ValidCode;
import com.lebang.entity.RequestHeaders;
import com.lebang.feign.WalkEarnedFeign;
import com.lebang.model.RequestUserInfo;
import com.lebang.model.back.Result;
import com.lebang.model.enums.buss.TaskCategory;
import com.lebang.service.MemberNewTaskService;
import com.lebang.service.MemberNoticeService;
import com.lebang.service.PointService;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 走路赚
 *
 * @Author: hwq
 * @Date: 2020/1/8 13:52
 * @Version 1.0
 */
@Api(tags = "走路赚")
@RestController
@RequestMapping("/app/ear/walk")
public class WalkEarnedApi {
    @Autowired
    private WalkEarnedFeign walkEarnedFeign;
    @Autowired
    private PointService pointService;
    @Autowired
    private MemberNewTaskService memberNewTaskService;
    @Autowired
    private MemberNoticeService memberNoticeService;

    @Require
    @ApiOperation("获取走路阶段完成信息")
    @PostMapping("/getWalkConfig")
    public JsonData getWalkConfig(HttpServletRequest request) {
        RequestUserInfo userInfo = new RequestUserInfo(request);
        return walkEarnedFeign.getWalkConfig(userInfo);
    }

    @Require
    @ApiOperation("走路赚配置记录添加")
    @PostMapping("/addDataRecord")
    public JsonData data(HttpServletRequest request, @ApiParam(name = "配置id") @RequestParam("configId") Integer configId) {
        RequestUserInfo userInfo = new RequestUserInfo(request);
        Result<Long> result = walkEarnedFeign.data(userInfo, configId);
        if (result.isSuccess()) {
            return Callback.success(result.getStatus(), result.getMessage(), result.getData());
        } else {
            return Callback.fail(result.getStatus(), result.getMessage());
        }
    }
    @ValidCode
    @Require
    @ApiOperation("走路赚领奖")
    @PostMapping("/pull")
    public JsonData pull(HttpServletRequest request,
                         @ApiParam(name = "配置id") @RequestParam("recordId") Long recordId) {
        if(recordId==null){
            return Callback.fail("参数非法", "参数非法");
        }
        RequestUserInfo userInfo = new RequestUserInfo(request);
        RequestHeaders headers = new RequestHeaders(request);
        Result<Integer> result = walkEarnedFeign.pull(userInfo, recordId);
        if (result.isSuccess()) {
            pointService.addPoint(userInfo.getMemberId(), result.getData(),
                    TaskCategory.WALK_EARNS, headers, "完成走路赚阶段奖励" + result.getData() + "金币");
            memberNewTaskService.addRecord(8890, headers.getUserid());
            memberNoticeService.addNoticeAndPush("走路赚", "完成走路赚阶段奖励" + result.getData()+ "金币", headers.getUserid(), headers);
            return Callback.success(result.getStatus(), result.getMessage(), result.getData());
        } else {
            return Callback.fail(result.getStatus(), result.getMessage());
        }
    }
}
